<template>
  <div class="register">
    <div class="mid-box">
      <div class="left-content transerve">
        <h1>欢迎来到虚拟宠物医院</h1>
        <h3>Welcome to Your Virtual Pet Hospital</h3>
      </div>
      <el-form
        class="right-content transerve"
        ref="registerForm"
        :model="registerForm"
        :rules="registerRules"
        :hide-required-asterisk="true">
        <h3 class="title">欢迎注册</h3>
        <el-form-item prop="username" label="用户名">
          <el-input
            v-model="registerForm.username"
            type="text"
            auto-complete="off"
            maxlength="5"
            show-word-limit
            placeholder="用户名">
          </el-input>
        </el-form-item>
        <el-form-item prop="email" label="邮 箱">
          <el-input
            v-model="registerForm.email"
            type="text"
            auto-complete="off"
            placeholder="邮箱">
          </el-input>
        </el-form-item>
        <el-form-item prop="password" label="密 码">
          <el-input
            v-model="registerForm.password"
            show-password
            type="password"
            auto-complete="off"
            placeholder="密码"
            @keyup.enter.native="handleRegister">
          </el-input>
        </el-form-item>
        <el-form-item prop="confirmPassword" label="确认密码">
          <el-input
            v-model="registerForm.confirmPassword"
            show-password
            type="password"
            auto-complete="off"
            placeholder="确认密码"
            @keyup.enter.native="handleRegister">
          </el-input>
        </el-form-item>
        <el-form-item prop="code" v-if="captchaEnabled" label="验证码">
          <el-input
            v-model="registerForm.code"
            auto-complete="off"
            placeholder="验证码"
            style="width: 67%"
            @keyup.enter.native="handleRegister">
          </el-input>
          <el-button
            type="primary"
            style="padding: 12px 6px !important; float: right"
            @click="sendVerification"
            >发送验证码</el-button
          >
        </el-form-item>
        <el-form-item style="width: 100%; text-align: center">
          <el-button
            :loading="loading"
            size="medium"
            type="primary"
            style="width: 85%"
            @click.native.prevent="handleRegister">
            <span v-if="!loading">注 册</span>
            <span v-else>注 册 中...</span>
          </el-button>
          <div style="float: right; margin-top: 10px">
            <router-link class="link-type" :to="'/'"
              >使用已有账户登录</router-link
            >
          </div>
        </el-form-item>
      </el-form>
    </div>
    <!--  底部  -->
    <div class="el-register-footer">
      <!-- <span>Copyright © 2018-2023 ruoyi.vip All Rights Reserved.</span> -->
    </div>
  </div>
</template>

<script>
import { sendVerificationAPI, signIn } from '../api/index'
import dentify from './yanzhengma.vue'
export default {
  name: 'Register',
  components: {
    dentify,
  },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.registerForm.password !== value) {
        callback(new Error('两次输入的密码不一致'))
      } else {
        callback()
      }
    }
    return {
      codeUrl: '',
      registerForm: {
        username: '',
        email: '',
        password: '',
        confirmPassword: '',
        code: '',
        tureCode: '',
      },
      registerRules: {
        username: [
          {
            required: true,
            message: '请输入用户名',
            trigger: 'blur',
          },
        ],
        email: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur',
          },
          {
            validator: function (rule, value, callback) {
              if (
                /^\w{1,64}@[a-z0-9\-]{1,256}(\.[a-z]{2,6}){1,5}$/i.test(
                  value
                ) == false
              ) {
                callback(new Error('邮箱格式错误'))
              } else {
                callback()
              }
            },
            trigger: 'blur',
          },
        ],
        password: [
          { required: true, trigger: 'blur', message: '请输入您的密码' },
          {
            min: 5,
            max: 20,
            message: '用户密码长度必须介于 5 和 20 之间',
            trigger: 'blur',
          },
        ],
        confirmPassword: [
          { required: true, trigger: 'blur', message: '请再次输入您的密码' },
          { required: true, validator: equalToPassword, trigger: 'blur' },
        ],
        // code: [{ required: true, trigger: 'change', message: '请输入验证码' }],
      },
      loading: false,
      captchaEnabled: true,
    }
  },
  mounted() {},
  methods: {
    sendVerification() {
      let data = {}
      data.email = this.registerForm.email
      this.$refs.registerForm.validate((valid) => {
        if (!valid) {
          this.$message.error('请将信息填写完整！')
        } else {
          sendVerificationAPI(data).then((res) => {
            console.log(res)
            this.registerForm.tureCode = res.data.data
            this.$message.success('发送成功！')
          })
        }
      })
    },
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          if (
            this.registerForm.code == this.registerForm.tureCode &&
            this.registerForm.tureCode != ''
          ) {
            this.loading = true
            let data = {}
            data.email = this.registerForm.email
            data.password = this.registerForm.password
            data.username = this.registerForm.username
            data.verification = this.registerForm.code
            signIn(data).then((res) => {
              console.log('5555555', res)
              if (res.data.code == 200) {
                this.$alert(
                  "<font color='red'>恭喜你，您的账号 " +
                    data.email +
                    ' 注册成功！</font>',
                  '系统提示',
                  {
                    dangerouslyUseHTMLString: true,
                    type: 'success',
                  }
                )
                  .then(() => {
                    this.$router.push('/')
                  })
                  .catch(() => {})
              } else if (res.data.code == -1) {
                this.$message.error(res.data.commonErrorCode.errorReason)
                this.reset()
                this.loading = false
              }
            })
          } else {
            this.$message.error('验证码错误')
          }
        }
      })
    },
    reset() {
      for (let i in this.registerForm) {
        this.registerForm[i] = ''
      }
    },
  },
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.link-type,
.link-type:focus {
  color: #337ab7;
  cursor: pointer;

  &:hover {
    color: rgb(32, 160, 255);
  }
}
.register {
  display: flex;
  // justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url('../assets/images/bg.png');
  background-size: 100% 100%;
}
.title {
  margin: 0px auto 30px auto;
  text-align: center;
  color: #707070;
  font-weight: 600;
  font-size: 27px;
}

.register-form {
  border-radius: 6px;
  background-color: rgba(255, 255, 255, 0.7);
  // width: 400px;
  width: 600px;
  // padding: 25px 25px 5px 25px;
  padding: 50px 100px 50px 100px;
  .el-input {
    height: 38px;
    input {
      height: 38px;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}
.register-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.register-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-register-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.register-code-img {
  height: 38px;
}
.mid-box {
  margin-top: 20px;
  width: 58%;
  height: 70%;
  margin-left: 20px;
  border-radius: 25px;
  padding: 30px 20px;
  display: flex;
  flex-direction: row;
  background-color: rgba(255, 255, 255, 0.4);
}
.transerve {
  flex: 1;
}
.left-content {
  margin-top: 10px;
  text-align: center;
  padding-top: 150px;
  height: 80%;
}
.right-content {
  margin-top: 10px;
  height: 95%;
  background-color: rgba(255, 255, 255, 0.5);
  border-radius: 25px;
  padding: 20px 30px;
  box-sizing: border-box;
}
.itemflex {
  display: flex;
  flex-direction: row;
}
::v-deep .el-form-item__content {
  display: inline-block;
  width: 80%;
}
::v-deep .el-form-item__label {
  width: 70px;
}
.el-form-item {
  margin-bottom: 15px;
}
</style>
